package com.lg.redis.simple;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.SerializationUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

@Component
public class RedisUtil implements JedisClient {
    @Autowired
	private RedisClient redisClient;

	@SuppressWarnings("unchecked")
	@Override
	public <T> T get(Object key, Class<T> type) {
		// TODO Auto-generated method stub
		Jedis jedis = redisClient.getJedis();
		try {
			byte[] bs =jedis.get(getKeyByte(key));
			return (T) SerializationUtils.deserialize(bs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			jedis.close();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public Object get(Object key) {
		// TODO Auto-generated method stub
		Jedis jedis = redisClient.getJedis();
		try {
			byte[] bs = jedis.get(getKeyByte(key));
			return  SerializationUtils.deserialize(bs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			jedis.close();
		}
		return jedis;
	}
	public byte[] getKeyByte(Object key) {
		if(key instanceof String) {
			return ((String) key).getBytes();
		}
		return SerializationUtils.serialize(key);
	}
	@Override
	public boolean set(Object key, Object value) {
		// TODO Auto-generated method stub
		Jedis jedis = redisClient.getJedis();
		try {
			jedis.set(getKeyByte(key), SerializationUtils.serialize(jedis));
			return true;
		} catch (Exception e) {
			return false;
		}finally {
			jedis.close();
		}
	}

	@Override
	public String get(String key) {
		// TODO Auto-generated method stub
		Jedis jedis = redisClient.getJedis();
		
		try {
			return jedis.get(key);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			jedis.close();
		}
		return key;
	}

	@Override
	public String set(String key, String value) {
		// TODO Auto-generated method stub
		return redisClient.getJedis().set(key, value);
	}

	@Override
	public String hget(String hkey, String key) {
		// TODO Auto-generated method stub
		return redisClient.getJedis().hget(hkey, key);
	}

	@Override
	public long hset(String hkey, String key, String value) {
		// TODO Auto-generated method stub
		Long hset = redisClient.getJedis().hset(hkey, key, value);
		return hset;
	}
	@Override
	public long hset(String hkey, Map<String, String> hash) {
		// TODO Auto-generated method stub
		Long hset = redisClient.getJedis().hset(hkey, hash);
		return hset;
	}

	@Override
	public long incr(String key) {
		// TODO Auto-generated method stub
		return redisClient.getJedis().incr(key);
	}

	@Override
	public long expire(String key, int seconds) {
		// TODO Auto-generated method stub
		return redisClient.getJedis().expire(key, seconds);
	}
	@Override
	public long expire(byte[] key, int second) {
		// TODO Auto-generated method stub
		return redisClient.getJedis().expire(key, second);
	}

	@Override
	public long ttl(String key) {
		// TODO Auto-generated method stub
		return redisClient.getJedis().ttl(key);
	}

	@Override
	public long del(String key) {
		// TODO Auto-generated method stub
		return redisClient.getJedis().del(key);
	}
	@Override
	public long del(Object key) {
		// TODO Auto-generated method stub
		return redisClient.getJedis().del(getKeyByte(key));
	}
	@Override
	public long del(String ... key) {
		// TODO Auto-generated method stub
		return redisClient.getJedis().del(key);
	}
	@Override
	public long hdel(String hkey, String key) {
		// TODO Auto-generated method stub
		
		return redisClient.getJedis().hdel(hkey, hkey);
	}

	
}
